//
//  UpdateCell.swift
//  appstore
//
//  Created by 王雷 on 2019/11/3.
//  Copyright © 2019 Roy. All rights reserved.
//

import UIKit

class UpdateCell: UITableViewCell {
    
    lazy var iconImageView = UIImageView()
    lazy var titleLabel = UILabel()
    lazy var detailTitleLabel = UILabel()
    lazy var updateItem = UIButton() // 更新按钮
    lazy var contentLabel = UILabel()
    lazy var sizeLabel = UILabel()
    lazy var moreItem = UIButton()
    
    var showMoreClosure:((_ para:String) -> (Void))? // 更多按钮闭包
    
    var model:UpdateModel! {
        didSet {
            iconImageView.image = UIImage(named: model.iconImageStr)
            titleLabel.text = model.appName
            detailTitleLabel.text = model.updateStr
            contentLabel.text = model.content
            
            moreItem.isHidden = model.showMore
            contentLabel.numberOfLines = model.showMore ? 0 : 1
            sizeLabel.isHidden = !model.showMore
            
            if model.showMore == true {
                sizeLabel.text = "Version " + model.version + " · " + model.size + " MB"
            }
            
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.initView()
    }
    
    func initView() -> Void {
        // icon
        self.iconImageView = UIImageView()
        self.iconImageView.layer.cornerRadius = 10.0
        self.iconImageView.layer.masksToBounds = true
        self.contentView.addSubview(self.iconImageView)
        
        // 更新按钮
        self.updateItem = UIButton(type: .custom)
        self.updateItem.setTitle("UPDATE", for: .normal)
        self.updateItem.setTitleColor(UIColor.blue, for: .normal)
        self.updateItem.layer.cornerRadius = 10.0
        self.contentView.addSubview(self.updateItem)
        
        // titleLabel
        self.titleLabel = UILabel()
        self.titleLabel.font = UIFont.systemFont(ofSize: 17.0)
        self.contentView.addSubview(self.titleLabel)
        
        // detailTitleLabel
        self.detailTitleLabel = UILabel()
        self.detailTitleLabel.font = UIFont.systemFont(ofSize: 13.0)
        self.detailTitleLabel.textColor = UIColor.gray
        self.contentView.addSubview(self.detailTitleLabel)
        
        // moreItem
        self.moreItem = UIButton(type: .custom)
        self.moreItem.setTitle("more", for: .normal)
        self.moreItem.setTitleColor(UIColor.blue, for: .normal)
        self.moreItem.titleLabel?.font = UIFont.systemFont(ofSize: 17.0)
        self.moreItem.addTarget(self, action: #selector(showMoreClickAction(button:)), for: .touchUpInside)
        self.contentView.addSubview(self.moreItem)
        
        // contentLabel
        self.contentLabel = UILabel()
        self.contentLabel.font = UIFont.systemFont(ofSize: 17.0)
        self.contentLabel.numberOfLines = 0
        self.contentView.addSubview(self.contentLabel)
        
        // sizeLabel
        self.sizeLabel = UILabel()
        self.sizeLabel.font = UIFont.systemFont(ofSize: 15.0)
        self.sizeLabel.textColor = UIColor.gray
        self.contentView.addSubview(self.sizeLabel)
        
        self.iconImageView.snp.makeConstraints { (make) in
            make.left.top.equalTo(20.0)
            make.width.height.equalTo(60.0)
        }
                
        self.updateItem.snp.makeConstraints { (make) in
            make.right.equalTo(-20.0)
            make.top.equalTo(35.0)
            make.width.equalTo(80.0)
            make.height.equalTo(30.0)
        }
                
        self.titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(iconImageView.snp.right).offset(10.0)
            make.top.equalTo(iconImageView.snp.top).offset(10.0)
            make.right.equalTo(updateItem.snp.left).offset(-10.0)
            make.height.equalTo(20.0)
        }
        
        self.detailTitleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(iconImageView.snp.right).offset(10.0)
            make.top.equalTo(titleLabel.snp.bottom).offset(5.0)
            make.right.equalTo(updateItem.snp.left).offset(-10.0)
            make.height.equalTo(20.0)
        }
        
        self.contentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(20.0)
            make.top.equalTo(iconImageView.snp.bottom).offset(30.0)
            make.right.equalTo(-60.0)
        }
        
        self.sizeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(20.0)
            make.top.equalTo(contentLabel.snp.bottom).offset(10.0)
            make.width.equalTo(contentLabel.snp.width)
            make.bottom.equalTo(-10.0)
        }
        
        self.moreItem.snp.makeConstraints { (make) in
            make.right.equalTo(-20.0)
            make.centerY.equalTo(contentLabel.snp.centerY)
            make.width.equalTo(40.0)
            make.height.equalTo(30.0)
        }
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    // Private Method
    @objc func showMoreClickAction(button:UIButton) -> Void {
        showMoreClosure?("")
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
